package com.fortune.bootup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

public abstract class AbstractController {

    protected static final Logger logger = LoggerFactory.getLogger("日志：");

    @Autowired
    protected HttpSession httpsession;

    @Autowired
    protected HttpServletResponse httpResponse;

    protected void setResponseFile(String filename) {
        httpResponse.setDateHeader("Expires", 0L);
        httpResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setContentType("application/octet-stream");
        httpResponse.setCharacterEncoding("UTF-8");
        String iso_filename = parseGBK(filename);
        httpResponse.setHeader("x-filename", iso_filename);
        httpResponse.setHeader("Content-Disposition", "attachment;filename=\"" + iso_filename + "\"");
    }

    private static String parseGBK(String sIn) {
        try {
            return java.net.URLEncoder.encode(sIn, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

}
